package com.untrance.shop.core.builder.vo;

import com.untrance.shop.common.vo.UserDetailsVO;
import com.untrance.shop.model.User;
import org.springframework.security.core.GrantedAuthority;

import java.util.ArrayList;
import java.util.List;

/**
 * @author eugene zadyra
 */
public class UserDetailsVOBuilder extends AbstractVOBuilder<User, UserDetailsVO> {

	public UserDetailsVOBuilder() {

	}

	@Override
	protected UserDetailsVO transform(User user) {
		UserDetailsVO vo = new UserDetailsVO();
		vo.setAccountNonLocked(!user.isLocked());
		if (user.getUserRole()!=null) {
			List<GrantedAuthority> roles = new ArrayList<GrantedAuthority>();
			roles.add(user.getUserRole());
			vo.setAuthorities(roles);
		}
		vo.setEnabled(user.isEnabled());
		vo.setPassword(user.getPassword());
		vo.setUsername(user.getUsername());
		return vo;
	}
}
